//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Windows.Controls;
using OpenLS.Core;

namespace OpenLS.Spreadsheet.Drawing
{
    class DraggedAnchorSideUndoUnit: AnchorFakeCompositeUndoUnit
    {
        private double leftAmount, rightAmount, topAmount, bottomAmount;

        public DraggedAnchorSideUndoUnit(Anchor anchor, double amount, Dock side): base (anchor)
        {
            switch(side)
            {
                case Dock.Bottom:
                    bottomAmount = amount;
                    break;
                case Dock.Left:
                    leftAmount = amount;
                    break;
                case Dock.Right:
                    rightAmount = amount;
                    break;
                case Dock.Top:
                    topAmount = amount;
                    break;
                default: throw new NotSupportedException();
            }
            
        }

        public override void Do()
        {
            if (leftAmount != 0) Anchor.DragSide(-leftAmount, Dock.Left);
            if (rightAmount != 0) Anchor.DragSide(-rightAmount, Dock.Right);
            if (topAmount != 0) Anchor.DragSide(-topAmount, Dock.Top);
            if (bottomAmount != 0) Anchor.DragSide(-bottomAmount, Dock.Bottom);
            
        }

        public override bool Merge(IUndoUnit unit)
        {
            var u = unit as DraggedAnchorSideUndoUnit;
            if (u != null && u.Anchor == Anchor)
            {
                leftAmount += u.leftAmount;
                rightAmount += u.rightAmount;
                topAmount += u.topAmount;
                bottomAmount += u.bottomAmount;
                return true;
            }
            return false;
        }
    }
}
